<template>
    <div>
        <!--卡片-->
        <el-card class="box-card">
            <!--定义面包屑-->
            <el-breadcrumb separator=">">
                <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
                <el-breadcrumb-item>客户管理</el-breadcrumb-item>
                <el-breadcrumb-item>会员等级</el-breadcrumb-item>
            </el-breadcrumb>
            <el-divider></el-divider>
            <!--表单的搜索框杀杀杀
                 :model="双向绑定表单的数据"
            -->
            <el-form :inline="true" :model="couponFormData"  class="demo-form-inline">
                <el-form-item >
                    <el-input v-model="couponFormData.name" placeholder="请输入优惠券名称"></el-input>
                </el-form-item>
                <el-form-item>
                    <el-button type="primary"@click="quere"> 查询</el-button>
                    <el-button type="primary" @click="insertClient">添加</el-button>
                </el-form-item>
            </el-form>

            <!--数据表格-->
            <template>
                <el-table
                        :data="tableData"
                        :border="true"
                        stripe
                        style="width: 100%">
                    <el-table-column
                            prop="id"
                            label="编号"
                            width="80">
                    </el-table-column>
                    <el-table-column
                            prop="type"
                            label="优惠券类型"
                            width="120"
                            :formatter="TypeFormat">
                    </el-table-column>
                    <el-table-column
                            prop="name"
                            label="优惠券名称"
                            width="200">
                    </el-table-column>
                    <el-table-column
                            prop="platform"
                            label="使用平台"
                            width="120"
                            :formatter="platFormat">
                    </el-table-column>
                    <el-table-column
                            prop="count"
                            label="数量">
                    </el-table-column>
                    <el-table-column
                            prop="amount"
                            label="金额">
                    </el-table-column>
                    <el-table-column
                            prop="perLimit"
                            label="每人限领张数"
                            width="120">
                    </el-table-column>
                    <el-table-column
                            prop="minPoint"
                            label="使用门槛"
                            width="120"
                            :formatter="minFormat">
                    </el-table-column>
                    <el-table-column
                            prop="startTime"
                            label="开始时间"
                            width="100">
                    </el-table-column>
                    <el-table-column
                            prop="endTime"
                            label="结束时间"
                            width="100">
                    </el-table-column>
                    <el-table-column
                            prop="useType"
                            label="使用类型"
                            :formatter="useFormat">
                    </el-table-column>
                    <el-table-column
                            prop="note"
                            label="备注">
                    </el-table-column>
                    <el-table-column
                            prop="publishCount"
                            label="发行数量">
                    </el-table-column>
                    <el-table-column
                            prop="useCount"
                            label="已使用数量"
                            width="100">
                    </el-table-column>
                    <el-table-column
                            prop="receiveCount"
                            label="领取数量">
                    </el-table-column>
                    <el-table-column
                            prop="enableTime"
                            label="可以领取的日期"
                            width="100">
                    </el-table-column>
                    <el-table-column
                            prop="code"
                            label="优惠码">
                    </el-table-column>
                    <el-table-column
                            prop="memberLevel"
                            label="可领取的会员类型"
                            :formatter="memberFormat">
                    </el-table-column>
                    <el-table-column label="操作" width="120">
                        <template slot-scope="scope">
                            <el-button type="primary" icon="el-icon-edit" circle @click="selectByVipId(scope.row.id)"></el-button>
                            <el-button type="danger" icon="el-icon-delete" circle @click="deleteViplevel(scope.row.id)"></el-button>
                        </template>
                    </el-table-column>
                </el-table>

                <!--                分页-->
                <el-pagination
                        @size-change="handleSizeChange"
                        @current-change="handleCurrentChange"
                        :current-page="currentPage"
                        :page-sizes="[5, 10, 15, 20]"
                        :page-size="pageSize"
                        layout="total, sizes, prev, pager, next, jumper"
                        :total="total">
                </el-pagination>
            </template>
        </el-card>
        <!--添加客户的弹出框-->
        <el-dialog
                title="录入会员"
                :visible.sync="editDialogVisible"
                width="30%">


            <!--            model:表单数据-->
            <el-form  label-width="130px" :model="couponForm"  :rules="rules" ref="couponForm" >
                <el-form-item label="id" style="display: none">
                    <el-input v-model="couponForm.id" ></el-input>
                </el-form-item>
                <el-form-item label="优惠券名称"  prop="name">
                    <el-input v-model="couponForm.name" placeholder="请输入优惠券名称"></el-input>
                </el-form-item>
                <el-form-item label="优惠券类型">
                    <el-select v-model="couponForm.type" placeholder="请输入优惠券类型"  >
                        <el-option
                                v-for="item in typeoptions"
                                :key="item.typevalue"
                                :label="item.label"
                                :value="item.typevalue">
                        </el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="使用平台">
                    <el-select v-model="couponForm.platform" placeholder="请选择使用平台">
                        <el-option
                                v-for="item in platoptions"
                                :key="item.platvalue"
                                :label="item.label"
                                :value="item.platvalue">
                        </el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="数量">
                    <el-input v-model="couponForm.count" placeholder="请输入数量" ></el-input>
                </el-form-item>
                <el-form-item label="金额">
                    <el-input v-model="couponForm.amount" placeholder="请输入金额"></el-input>
                </el-form-item>
                <el-form-item label="每人限领张数">
                    <el-input v-model="couponForm.perLimit" placeholder="请输入每人限领张数" ></el-input>
                </el-form-item>
                <el-form-item label="使用门槛">
                    <el-select v-model="couponForm.minPoint" placeholder="请选择使用门槛" >
                        <el-option
                                v-for="item in minoptions"
                                :key="item.minvalue"
                                :label="item.label"
                                :value="item.minvalue">
                        </el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="开始时间">
                    <el-input v-model="couponForm.startTime" type="date" placeholder="请输入开始时间"></el-input>
                </el-form-item>
                <el-form-item label="结束时间">
                    <el-input v-model="couponForm.endTime" type="date" placeholder="请输入结束时间" ></el-input>
                </el-form-item>
                <el-form-item label="使用类型">
                    <el-select v-model="couponForm.useType" placeholder="请输入使用类型" >
                        <el-option
                                v-for="item in useoptions"
                                :key="item.usevalue"
                                :label="item.label"
                                :value="item.usevalue">
                        </el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="发行数量">
                    <el-input v-model="couponForm.publishCount" placeholder="请输入发行数量" ></el-input>
                </el-form-item>
                <el-form-item label="可以领取的日期">
                    <el-input v-model="couponForm.enableTime" type="date" placeholder="请输入领取的日期" ></el-input>
                </el-form-item>
                <el-form-item label="优惠码">
                    <el-input v-model="couponForm.code" placeholder="请输入优惠码"></el-input>
                </el-form-item>
                <el-form-item label="可领取的会员类型">
                        <el-select v-model="couponForm.memberLevel"  placeholder="请选择可领取的会员类型" >
                            <el-option
                                    v-for="item in memoptions"
                                    :key="item.value"
                                    :label="item.label"
                                    :value="item.value">
                            </el-option>
                        </el-select>
                </el-form-item>

            </el-form>

            <span slot="footer" class="dialog-footer">
                <el-button @click="editDialogVisible = false">取 消</el-button>
                <el-button type="primary" @click="formSubmit">确 定</el-button>
              </span>
        </el-dialog>
    </div>
</template>

<script>
    export default {
        name: "Coupon",
        data(){
            //唯一性校验
            var Id = this.id
            var same = {id: '', same: ''}
            var that = this
            var validatorname = function (rule, value, callback) {
                same.id = Id;
                same.same = value;
                that.$http.post(`/manage/coupon/uniqueCheck`, same).then(function (response) {
                    if (response.data.result.type) {
                        callback();
                    } else {
                        callback("菜单名重复");
                    }
                });
            }
            return{
                editDialogVisible:false,
                pageSize:5,
                currentPage:1,
                tableData:[],
                couponFormData: {},
                total:0,
                id:'',
                couponForm:{
                    name:'',
                },
                //name唯一
                rules:{
                    name: [
                        { required: true, message: '请输入等级名称', trigger: 'blur' },
                        {validator: validatorname, trigger: 'blur'}
                    ],
                },

                value: '',
                memoptions: [{
                    value: 0,
                    label: '无限时'
                }, {
                    value: 1,
                    label: '七天'
                },],
                minvalue:'',
                minoptions: [{
                    minvalue: 0,
                    label: '无门槛'
                }, {
                    minvalue: 1,
                    label: '黄金会员'
                },],
                usevalue:'',
                useoptions: [{
                    usevalue: 0,
                    label: '全场通用'
                }, {
                    usevalue: 1,
                    label: '指定分类'
                },{
                    usevalue: 2,
                    label: '指定车辆'
                },],
                minvalue:'',
                minoptions: [{
                    minvalue: 0,
                    label: '无门槛'
                }, {
                    minvalue: 1,
                    label: '黄金会员'
                },],
                 platvalue:'',
                platoptions:[{
                    platvalue: 0,
                    label: '全部'
                }, {
                    platvalue: 1,
                    label: '移动'
                },{
                    platvalue: 2,
                    label: 'PC'
                },],
                typevalue:'',
                typeoptions: [{
                    typevalue: 0,
                    label: '全场赠券'
                }, {
                    typevalue: 1,
                    label: '会员赠券'
                },{
                    typevalue: 2,
                    label: '消费赠券'
                },],

            }
        },
        created() {
            this.initcouponTable()
        },
        methods:{
            initcouponTable(){
                var that = this;
                this.$http.post(`/manage/coupon/initCoupon/${this.currentPage}/${this.pageSize}`,this.couponFormData).then(function (resp) {
                    that.tableData=resp.data.result.records;
                    that.total = resp.data.result.total;
                })
            },

            TypeFormat: function( row, column) {
                return row.type == '1' ? "会员增送" : row.type == '0' ? "全场赠送" : "111";
            },
            platFormat: function( row, column) {
                return row.platform == '1' ? "移动端可用" : row.platform == '0' ? "全平台可用" : "PC端可用";
            },
            minFormat: function( row, column) {
                return row.minPoint == '1' ? "黄金会员" : row.minPoint == '0' ? "无门槛" : "星耀会员";
            },
            useFormat: function( row, column) {
                return row.useType == '1' ? "指定分类" : row.useType == '0' ? "全场通用" : "指定车辆";
            },
            memberFormat: function( row, column) {
                return row.memberLevel == '1' ? "三天" : row.memberLevel == '0' ? "无限时" : "七天";
            },


            formSubmit(){
                var that=this;
                if(this.couponForm.id!=null){
                    this.$http.put("/manage/coupon/updateCoupon",this.couponForm).then(function (returnResult) {
                        that.editDialogVisible=false;
                        that.couponForm={};
                        that.initcouponTable();
                    })
                }else {
                    this.$http.post("/manage/coupon/insertCoupon", this.couponForm).then(function (returnResult) {
                        that.editDialogVisible = false;
                        that.initcouponTable();
                    })
                }
            },

            selectByVipId(id){
                var that=this;
                this.$http.post(`/manage/coupon/selectByCouponId/${id}`).then(function (returnResult) {
                    that.editDialogVisible=true;
                    console.log(returnResult)
                    that.couponForm=returnResult.data.result.couponInfo;
                })
            },

            handleSizeChange(v){
                this.pageSize = v;
                this.currentPage();
            },
            //切换页码时触发该方法
            handleCurrentChange(v){
                this.currentPage = v;
                this.initcouponTable();
            },
            quere() {
                this.initcouponTable()
            },

            insertClient(){
                this.editDialogVisible=true;
                this.couponForm={};
            },
            updateViplevel(){
                this.editDialogVisible=true;
                this.couponForm={};
                that.couponForm=returnResult.data.result.couponInfo;
            },
            deleteViplevel(id){
                var that=this;
                this.$http.delete(`/manage/coupon/deleteCoupon/${id}`, this.couponForm).then(function (resp) {
                    that.initcouponTable();
                })
            },

        }

    }
</script>

<style scoped>

</style>